<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<form action="" method="post" enctype="multipart/form-data" name="form1">
  <p>&nbsp;</p>
  <p>
    <input type="file" name="file" id="file">
    <input type="submit" name="upload" id="upload" value="Submit">
  </p>
  <p>&nbsp;</p>
</form>
<?php 
include_once($_SERVER['DOCUMENT_ROOT'].'/QL MBA/Models/DonVi.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/QL MBA/PHPExcel_1.7.9_doc/Classes/PHPExcel.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/QL MBA/PHPExcel_1.7.9_doc/Classes/PHPExcel/IOFactory.php');

if( isset($_POST['upload']) ){
	if( $_FILES["file"]["name"] != "" ){

$objPHPExcel = PHPExcel_IOFactory::load($_FILES["file"]["tmp_name"]);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
     $worksheetTitle = $worksheet->getTitle();
     $highestRow = $worksheet->getHighestRow(); // e.g. 10
     $highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
     $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
}

//
$nrColumns = ord($highestColumn) - 64;


//
echo "File ".$worksheetTitle." has ";
echo $nrColumns . ' columns';
echo ' y ' . $highestRow . ' rows.';
//
echo 'Data: <table width="100%" cellpadding="3" cellspacing="0"><tr>';
for ($row = 1; $row <= $highestRow; ++ $row) {
   echo '<tr>';
   for ($col = 0; $col < $highestColumnIndex; ++ $col) {
       $cell = $worksheet->getCellByColumnAndRow($col, $row);
       $val = $cell->getValue();
       if($row === 1)
            echo '<td style="background:#000; color:#fff;">' . $val . '</td>';
       else
           echo '<td>' . $val . '</td>';
   }
echo '</tr>';
}
echo '</table>';
// quang vao csdl
for ($row = 2; $row <= $highestRow; ++ $row) {
	$val=array();
for ($col = -1; $col < $highestColumnIndex;  $col++) {
   $cell = $worksheet->getCellByColumnAndRow($col, $row);
   $val[] = $cell->getValue();
 }
 
  // $sql="insert into tablename(column1, column2, column3, column4, column5, column6)
  // values('".$val[1] . "','" . $val[2] . "','" . $val[3]. "','" . $val[4]. "','" . $val[5]. "','" . $val[6]. "')";
//Run your mysql_query
	$donvi = new DonVi();
    $madv = $val[1];
    $tendv = $val[2];
	$diachidv = $val[3];
	$lienhedv = $val[4];
	$tentk = $val[1];
	$matkhau = $val[1];
	$quyen = 1;
	
	
    $donvi->setMaDV($madv);
    $donvi->setTenDV($tendv);
	$donvi->setMaDV($madv);
    $donvi->setTenDV($tendv);
	$donvi->setDiaChiDV($diachidv);
	$donvi->setLienHeDV($lienhedv);
	$donvi->setTenTK($tentk);
	$donvi->setMatKhau($matkhau);
	$donvi->setQuyen($quyen);  

    if ($donvi->isExits("don_vi","MA_DON_VI='".$donvi->getMaDV()."'") <= 0  ){
            if ($donvi->themDV() > 0){
        			echo "<td class='level_1_$ci' align='center'>OK</td></tr>";
            }
            else{
                	echo "<td class='level_1_$ci' align='center'>bị trùng</td></tr>";
            } 
        }
        else{
            //khoa da ton tai
            	echo "<td class='level_1_$ci' align='center'>bị trùng</td></tr>";
        }
}



	} else { echo "Chon file excel de upload!";}

}
?>


</body>
</html>

